package com.e.dianshang.controller;

import com.e.dianshang.pojo.Order;
import com.e.dianshang.service.OrderService;
import com.e.dianshang.utils.BusinessException;
import com.e.dianshang.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;


@RestController
@Api(value = "购物车",tags = "购物车的api")
public class OrderController {

    @Resource
    OrderService orderService;

    @ApiOperation(value = "添加购物车的接口",notes = "添加购物车",httpMethod = "POST",response = String.class)
    @RequestMapping(value = "/insertCar",method = RequestMethod.POST)
    public Map insertCar(@RequestBody Order order) throws BusinessException {

        JsonResult jsonResult = new JsonResult();

        Integer quantity = order.getQuantity();
        if (quantity<1){
            throw new BusinessException("至少添加一件商品");
        }
        int i = orderService.insert(order);

        if (i>0){
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");

        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("系统开小差了");
        }

        return jsonResult.getValues();
    }


//    @GetMapping("/getList")
//    @ApiOperation(value = "购物车列表接口",httpMethod = "GET")
//    @ApiImplicitParam(name = "openid",value = "用户微信ID",required = true,dataType = "string",paramType = "query")
//    public Map getList(@RequestParam("openid") String openid) throws BusinessException {
//
//        JsonResult jsonResult=new JsonResult();
//
//        List<Map> list = orderService.getList(openid);
//
//        if (!list.isEmpty()){
//            jsonResult.setCode("0");
//            jsonResult.setData(list);
//        }else {
//            throw new BusinessException("你还没有添加商品哦");
//        }
//
//        return jsonResult.getValues();
//
//    }
//
//    @PostMapping("/delete")
//    @ApiOperation(value = "删除购物车商品",httpMethod = "POST")
//    public Map delete(@RequestBody @ApiParam(value = "删除商品的pojo信息 数组",required = true) Map<String, List> id) throws BusinessException {
//
//        List ids = id.get("id");
//        System.out.println(id);
//
//        JsonResult jsonResult=new JsonResult();
//
//        int delete = orderService.deleteBatch(ids);
//
//        if (delete>0){
//            jsonResult.setCode("0");
//            jsonResult.setMsg("删除成功！！");
//        }else {
//            throw new BusinessException("删除失败！！");
//        }
//
//        return jsonResult.getValues();
//
//    }
//
//    @PostMapping("/updateQuantity")
//    @ApiOperation(value = "修改购物车商品件数",httpMethod = "POST")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id",value = "购物车商品id",required = true,dataType = "int",paramType = "query"),
//            @ApiImplicitParam(name = "quantity",value = "修改商品数量 +为固定1，-为固定-1",required = true,dataType = "int",paramType = "query")
//    })
//    public Map getTktByName(@RequestParam("id") Integer id,@RequestParam("quantity") Integer quantity) throws BusinessException {
//
//        JsonResult jsonResult=new JsonResult();
//        Order select = orderService.select(id);
//        Integer quantity1 = select.getQuantity();
//        Order order =new Order();
//        order.setId(id);
//        order.setQuantity(quantity1+quantity);
//        int i = orderService.updateQuantity(order);
//
//        if (i==1){
//            jsonResult.setCode("0");
//        }else {
//            throw new BusinessException("修改失败");
//        }
//
//        return jsonResult.getValues();
//
//    }
//
//    @PostMapping("/updateGuiGe")
//    @ApiOperation(value = "修改已加入购物车商品规格信息",httpMethod = "POST")
//    public Map updateGuiGe(@RequestBody @ApiParam(value = "修改规格属性的json信息 数组",required = true) Order order) throws BusinessException {
//
//
//        JsonResult jsonResult=new JsonResult();
//
//        int i = orderService.updateGuiGe(order);
//
//        if (i>0){
//            jsonResult.setCode("0");
//            jsonResult.setMsg("选择成功！！");
//        }else {
//            throw new BusinessException("选择失败！！");
//        }
//
//        return jsonResult.getValues();
//
//    }

}
